package pack1;

import java.io.IOException;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.swim.entity.Collaboration;
import com.swim.session.CollaborationRemote;
import com.swim.session.FeedbackRemote;

public class NewFeedbackServlet extends HttpServlet {

	private static final long serialVersionUID = 2088037365143035505L;

	public NewFeedbackServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		if (session.getAttribute("authenticated") == null
				|| !(boolean) session.getAttribute("authenticated")) {
			response.sendRedirect("index.jsp");
			return;
		}

		String collabIdStr = request.getParameter("id");
		int collabID = Integer.parseInt(collabIdStr);

		try {
			InitialContext jndiContext = (InitialContext) Util
					.getInitialContext();

			Object ref1 = jndiContext.lookup("CollaborationBeanJNDI");
			CollaborationRemote r = (CollaborationRemote) ref1;
			Collaboration c = r.getCollaboration(collabID);
			
			if (!c.getEmailTo().getEmail()
					.equals(session.getAttribute("authenticated-email"))) {

				response.sendRedirect("index.jsp");

			} else {
				request.setAttribute("id", collabIdStr);
				RequestDispatcher dispatcher = request
						.getRequestDispatcher("/WEB-INF/auth/newfb.jsp");
				dispatcher.forward(request, response);
			}
		} catch (Exception e) {
		}
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		if (session.getAttribute("authenticated") == null
				|| !(boolean) session.getAttribute("authenticated")) {
			response.sendRedirect("index.jsp");
			return;
		}


		String collabIdStr = request.getParameter("id");
		int collabID = Integer.parseInt(collabIdStr);

		try {
			InitialContext jndiContext = (InitialContext) Util
					.getInitialContext();

			Object ref1 = jndiContext.lookup("CollaborationBeanJNDI");
			CollaborationRemote r = (CollaborationRemote) ref1;
			Collaboration c = r.getCollaboration(collabID);
			
			if (!c.getEmailTo().getEmail()
					.equals(session.getAttribute("authenticated-email"))) {

				response.sendRedirect("index.jsp");

			} else {
				String desc = request.getParameter("desc");
				String rating = request.getParameter("rating");
				ref1 = jndiContext.lookup("FeedbackBeanJNDI");
				FeedbackRemote fRemote = (FeedbackRemote) ref1;
				fRemote.addFeedback(collabID, Integer.parseInt(rating), desc);

				
				request.setAttribute("title","Feedback saved");
				request.setAttribute("message","Go <a href=\"collaborations.jsp\">back</a> to your collaborations or <a href=\"index.jsp\">home</a>.");
				RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/target.jsp");
				dispatcher.forward(request, response); 
			}
		} catch (Exception e) {
		}
	}
}
